<extend name="layout/edit" />

<block name="HEADER">
    <style type="text/css">
         .layui-form-selected dl{
            padding-bottom: 5px;
         }
         .layui-form-select dl{
            max-height: 270px;
         }
    </style>
</block>

<block name="CONTENT">

	<control:form id="settlementForm"/>
    
</block>

<block name="FOOTER">
   <script type="text/javascript">
	    var  page=(function(ns){
	    	
	    	//初始化
	    	ns.init = function(){
	    		ns.settlementInfo =  parent.page.settlementInfo;	    		
	    		ns.settlement();

                settlementForm.on("input",'use_offline_price',function(e){
                    var num =  ( 
                    	Number(settlementForm.getValue('use_offline_price'))
                    	+ Number(settlementForm.getValue('balancet'))
                    	- Number(settlementForm.getValue('receivable'))
                    ).toFixed(2);
                    settlementForm.setValue('changes',num);
				});

                //选中优惠券切换事件
                settlementForm.on("input",'coupon_id',function(e){
                	ns.settlement( settlementForm.getValue('coupon_id') );
                });
	    	}

            //结算页面的填充
            ns.settlement=function(coupon_id){
            	
                $.ajaxs('/admin/v1/order/getSettlementInfo',{
                    uid:ns.settlementInfo.uid,
                    coupon_id: coupon_id || 0,
                    data:  JSON.stringify($.map(ns.settlementInfo.groods,function(h){
                    	return {
                    		id:h.specs_id,
                    		goods_id:h.goods_id,
                    		num:h.cart_num,
                    		is_proj:h.is_proj,
                    		is_yg_proj:h.is_yg_proj,
                    		staff_id:h.staff_id,
                    		scale:h.scale
                    	}
                    }))
                }).then(function(res){
                	var data = res.data;
                    //填充结算信息
                    settlementForm.setValue('total_money',data.priceGroup.totalSalePrice);  //订单总金额
                    settlementForm.setValue('give_balance',data.balanceInfo.deductPrice);   //赠送余额抵扣
                    settlementForm.setValue('point_deduct',data.integralInfo.deductPrice);   //积分抵扣
                    settlementForm.setValue('receivable',data.priceGroup.payPrice);             //应收金额
                    var receipts = (data.priceGroup.payPrice - data.amountInfo.deductPrice).toFixed(2);
                    settlementForm.setValue('receipts',receipts);                //实收金额
                    settlementForm.setValue('balancet',data.amountInfo.deductPrice);         //余额支付
					settlementForm.setValue('orderKey',data.orderKey);
					settlementForm.setValue('remark',ns.settlementInfo.remarks);
					 
                    //填充优惠券
                    var couponList = $.map(data.couponList,function(h,index){
                        h.value = h.id;
                        h.label = h.name ;
                        return h;
                    });
                    couponList.unshift({ value:"", label:""});
                    settlementForm.addOption('coupon_id',couponList);
                    settlementForm.setValue("coupon_id",data.couponInfo.id);
                    
                 	//优惠券分两种
                    if(data.couponInfo){
                        if(data.couponInfo.type=='满折'){
                            settlementForm.setValue('coupon_discount',data.couponInfo.fracture+'折');  
                        }
                        if(data.couponInfo.type=='满减'){
                            settlementForm.setValue('coupon_discount',data.couponInfo.price+'元');             
                        }
                    }else{
                    	settlementForm.setValue('coupon_discount','');       
                    }
                    
                    //活动优惠分两种
                    if(data.activityInfo) {
                        if (data.activityInfo.benefit_mode == 1) {
                            settlementForm.setValue('activity_discount',data.activityInfo.deductPrice + '元');                  
                        }
                        if (data.activityInfo.benefit_mode == 2) {
                            settlementForm.setValue('activity_discount', data.activityInfo.benefit_discount + '折');                  
                        }
                    }else{
                    	settlementForm.setValue('activity_discount','');       
                    }
                    
                });
            }

	        //确认结算
	        ns.save=function(){
	        	
	        	if(settlementForm.getValue('use_offline_price') < settlementForm.getValue('receipts')){
	        		return $.toast(0,'“线下收款”需要输入大于等于“还需收款”，才能点击“确认结算”!');
	        	}
	        	
                $.ajaxs('/admin/v1/order/confirm',{
                    orderKey: settlementForm.getValue('orderKey'),
                    mark:settlementForm.getValue('remark'),
                    uid:ns.settlementInfo.uid,
                    data:  JSON.stringify($.map(ns.settlementInfo.groods,function(h){
                    	return {
                    		id:h.specs_id,
                    		proj_id:h.proj_id,
                    		goods_id:h.goods_id,
                    		num:h.cart_num,
                    		is_proj:h.is_proj,
                    		is_yg_proj:h.is_yg_proj,
                    		staff_id:h.staff_id,
                    		scale:h.scale
                    	}
                    }))
                }).then(function(res){
                    settlementForm.opener.$.toast(1,"已成功开单！")
                    settlementForm.opener.page.reload();
                    settlementForm.opener.grid.load();
                    $.close();
				});
	        }
	        
	        return ns;
	    }({}));
	    
	    
	    $(function(){ page.init() });
   </script>
</block>



